package com.horoscope.astrology.zodiac.signs.free.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.MainActivity
import com.horoscope.astrology.zodiac.signs.free.adapter.ArticlePageFragmentAdapter
import com.horoscope.astrology.zodiac.signs.free.fragment.article.ArticleSubFragment

/**
 * 文章
 * */
class ArticleFragment : BaseFragment() {

    private var mAdapter: ArticlePageFragmentAdapter? = null
    private lateinit var mTabLayout: TabLayout
    private lateinit var mViewPager: ViewPager
    private var currentItem = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_article, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
    }

    private fun initView(view: View) {
        mTabLayout = view.findViewById(R.id.tab_layout_article)
        mViewPager = view.findViewById(R.id.view_pager_article)

        mAdapter = ArticlePageFragmentAdapter(activity,2,  childFragmentManager)
        mViewPager.adapter = mAdapter
        mViewPager.offscreenPageLimit = 3

        // 绑定TabLayout ViewPager
        /**
         * 第一个是从VIewPager中获取TabLayout的Title
         * 第二个是ViewPager滑动时设置TabLayout的Title和indicator
         * 第三个是点击TabLayout时ViewPager相应变化
         * **/
        mTabLayout.setupWithViewPager(mViewPager)
        setTabView()

        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            // 滑动
            override fun onPageSelected(position: Int) {
                currentItem = position
                val titleId = ArticlePageFragmentAdapter.mTitleArticle[currentItem]
                if (activity is MainActivity) {
                    mAdapter!!.setCurrentPosition(position)
                    (activity as MainActivity).setArticleCurrentTitle(getString(titleId))
                }
            }

        })

    }

    fun notifyAd() {
        mAdapter?.notifyAd()
    }

    fun doubleClick() {
        if (mAdapter == null) return
        val fragment = mAdapter?.getItem(currentItem) as ArticleSubFragment
        fragment.doubleClick()
    }

    private fun setTabView() {
        mTabLayout.getTabAt(0)!!.customView =
            getTabView(resources.getString(R.string.fragment_hot_title))
        mTabLayout.getTabAt(1)!!.customView =
            getTabView(resources.getString(R.string.fragment_love_title))
        mTabLayout.getTabAt(2)!!.customView =
            getTabView(resources.getString(R.string.text_career))
        mTabLayout.getTabAt(3)!!.customView =
            getTabView(resources.getString(R.string.fragment_other_title))
    }

    // 自定义每个Tab
    private fun getTabView(title: String): View {
        val v = LayoutInflater.from(activity).inflate(R.layout.tab_item_view, null)
        val textView = v!!.findViewById<TextView>(R.id.text_view_tab)
        textView.text = title
        val new = v.findViewById<TextView>(R.id.text_view_new)
        new.visibility = View.INVISIBLE
        return v
    }
}
